package com.weaver.openapi.service;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.weaver.openapi.pojo.MessageResult;
import com.weaver.openapi.pojo.room.params.RoomVo;
import com.weaver.openapi.pojo.room.res.RoomResultVo;
import com.weaver.openapi.util.HostUtil;

import java.util.HashMap;
import java.util.Map;

public class RoomService {
    public static RoomResultVo createRoomV2(RoomVo roomVo, String host, Map<String, String> headers) {
        host = HostUtil.beforeRequestCheckHeaders(host, headers, "application/x-www-form-urlencoded");
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("access_token", roomVo.getAccessToken());
        paramMap.put("userid", roomVo.getUserid());
        paramMap.put("name", roomVo.getName());
        paramMap.put("place", roomVo.getPlace());
        paramMap.put("galleryful", roomVo.getGalleryful());
        paramMap.put("projector", roomVo.isProjector());
        paramMap.put("remarks", roomVo.getRemarks());
        paramMap.put("locked", roomVo.isLocked());
        String resJson = ((HttpRequest) HttpRequest.post(host + "/room/v2/createRoom").form((Map) paramMap).headerMap((Map) headers, true)).execute().body();
        return (RoomResultVo) JSONObject.parseObject(resJson, (Class) RoomResultVo.class);
    }

    public static RoomResultVo updateRoomV2(RoomVo roomVo, String host, Map<String, String> headers) {
        host = HostUtil.beforeRequestCheckHeaders(host, headers, "application/x-www-form-urlencoded");
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("access_token", roomVo.getAccessToken());
        paramMap.put("userid", roomVo.getUserid());
        paramMap.put("id", roomVo.getId());
        paramMap.put("propertyName", roomVo.getPropertyName());
        paramMap.put("propertyValue", roomVo.getPropertyValue());
        String resJson = ((HttpRequest) HttpRequest.post(host + "/room/v2/updateRoom").form((Map) paramMap).headerMap((Map) headers, true)).execute().body();
        return (RoomResultVo) JSONObject.parseObject(resJson, (Class) RoomResultVo.class);
    }

    public static MessageResult softDeleteRoomV2(RoomVo roomVo, String host, Map<String, String> headers) {
        host = HostUtil.beforeRequestCheckHeaders(host, headers, "application/x-www-form-urlencoded");
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("access_token", roomVo.getAccessToken());
        paramMap.put("userid", roomVo.getUserid());
        paramMap.put("id", roomVo.getId());
        String resJson = ((HttpRequest) HttpRequest.post(host + "/room/v2/softDeleteRoom").form((Map) paramMap).headerMap((Map) headers, true)).execute().body();
        return (MessageResult) JSONObject.parseObject(resJson, (Class) MessageResult.class);
    }

    public static RoomResultVo queryRoomsV2(RoomVo roomVo, String host, Map<String, String> headers) {
        host = HostUtil.beforeRequestCheckHeaders(host, headers, "application/x-www-form-urlencoded");
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("access_token", roomVo.getAccessToken());
        paramMap.put("userid", roomVo.getUserid());
        paramMap.put("id", roomVo.getId());
        paramMap.put("name", roomVo.getName());
        paramMap.put("place", roomVo.getPlace());
        paramMap.put("galleryful", roomVo.getGalleryful());
        paramMap.put("projector", roomVo.isProjector());
        paramMap.put("remarks", roomVo.getRemarks());
        paramMap.put("locked", roomVo.isLocked());
        String resJson = ((HttpRequest) HttpRequest.post(host + "/room/v2/queryRooms").form((Map) paramMap).headerMap((Map) headers, true)).execute().body();
        return (RoomResultVo) JSONObject.parseObject(resJson, (Class) RoomResultVo.class);
    }

    public static RoomResultVo queryEventsV2(RoomVo roomVo, String host, Map<String, String> headers) {
        host = HostUtil.beforeRequestCheckHeaders(host, headers, "application/x-www-form-urlencoded");
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("access_token", roomVo.getAccessToken());
        paramMap.put("userid", roomVo.getUserid());
        paramMap.put("start", roomVo.getStart());
        paramMap.put("end", roomVo.getEnd());
        String resJson = ((HttpRequest) HttpRequest.post(host + "/room/v2/queryEvents").form((Map) paramMap).headerMap((Map) headers, true)).execute().body();
        return (RoomResultVo) JSONObject.parseObject(resJson, (Class) RoomResultVo.class);
    }
}
